package com.wanxi.controller.product;

import com.wanxi.domain.PageModel;
import com.wanxi.domain.ResultModel;
import com.wanxi.domain.product.ProductModel;
import com.wanxi.service.product.IProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("product")
@RestController
@CrossOrigin(allowCredentials = "true", origins = "http://localhost:63342", allowedHeaders = "*")//允许跨域请求
public class ProductController {

    @Autowired
    IProductService productService;

    @PostMapping("upload")
    public String uploadFile(HttpServletRequest req, MultipartFile file) throws IOException {
        /*得到上传的原始文件名*/
        String originalFilename = file.getOriginalFilename();

        /*得到该目录在服务器中的绝对路径*/
        String realPath = req.getServletContext().getRealPath("/upload");
        System.out.println(realPath);

        File file1 = new File(realPath);
        /*判断该路径是否存在*/
        if (!(file1.exists())) {
            /*递归创建文件夹*/
            file1.mkdirs();
        }

        /*使用uuid防止文件重名*/
        String s = UUID.randomUUID().toString();
        StringBuilder stringBuilder = new StringBuilder();
        /*最终存到服务器的文件名*/
        String filename = stringBuilder.append(s).append("_").append(originalFilename).toString();

        /*将文件存入*/
        file.transferTo(new File(realPath, filename));

        /*ResultModel resultModel = new ResultModel();
        resultModel.setCode(0);
        resultModel.setData(File.separator+"back_page"+File.separator+ "upload" +File.separator + filename);
*/

        return File.separator+"back"+File.separator+ "upload" +File.separator + filename;

    }

    /*查询所有*/
    @GetMapping("findAll")
    public List<ProductModel> findAll(@Param("page")int page, @Param("limit")int limit){

        //当前页
        int curr = (page - 1) * limit;

        List<ProductModel> resultModel = productService.findAll(curr,limit);


        return resultModel;


    }


    /*根据产品id查新产品信息*/
    @GetMapping("findById")
    public ProductModel findById(@Param("id") int id){

        ProductModel resultModel = productService.findById(id);

        return resultModel;

    }

    /*编辑产品*/
    @PostMapping("update")
    public int updateProduct(@RequestBody ProductModel productModel){

        int resultModel =  productService.update(productModel);


        return resultModel;

    }

    /*根据id删除产品*/
    @PostMapping("delete")
    public int deleteById(@RequestBody ProductModel productModel){

        int resultModel =  productService.deleteByid(productModel.getId());

        return resultModel;

    }

    /*新增产品*/
    @PostMapping("insert")
    public int insertProduct(@RequestBody ProductModel productModel){

        int resultModel = productService.insert(productModel);

        return resultModel;

    }

}
